package com.mjh.dao;

import org.apache.log4j.Logger;
import com.mjh.bean.SystemConfig;
import com.mjh.util.ResourceUtil;
import com.mjh.util.XMLParser;

public class SystemConfigDAO {

	public static Logger log = Logger.getLogger(SystemConfigDAO.class);
	
	public static XMLParser systemXML;
	
	static {
		systemXML = new XMLParser(ResourceUtil.getRealPath("com/mjh/config/system.xml"));
	}
	
	private void saveChange(){
		systemXML.wirteXML();
	}
	
	public void setValue(String nodeName, String value){
		systemXML.setChildNodeValue(nodeName, value);
		saveChange();
	}

	public String getValue(String nodeName){
		return systemXML.getChildNodeValue(nodeName);
	}

	public void setSystemConfig(SystemConfig config){
		systemXML.setChildNodeValue("articleListPageSize", String.valueOf(config.getArticleListPageSize()));
		systemXML.setChildNodeValue("articleDetailPageSize", String.valueOf(config.getArticleDetailPageSize()));
		systemXML.setChildNodeValue("discussPageSize", String.valueOf(config.getDiscussPageSize()));
		systemXML.setChildNodeValue("debug", String.valueOf(config.isDebug()));
		systemXML.setChildNodeValue("recordClick", String.valueOf(config.isRecordClick()));
		systemXML.setChildNodeValue("imgFolder", config.getImgFolder());
		saveChange();
	}
	
	public SystemConfig getSystemConfig(){
		SystemConfig config = new SystemConfig();
		config.setDebug(Boolean.parseBoolean(getValue("debug")));
		config.setArticleListPageSize(Integer.parseInt(getValue("articleListPageSize")));
		config.setArticleDetailPageSize(Integer.parseInt(getValue("articleDetailPageSize")));
		config.setDiscussPageSize(Integer.parseInt(getValue("discussPageSize")));
		config.setRecordClick(Boolean.parseBoolean(getValue("recordClick")));
		config.setImgFolder(getValue("imgFolder"));
		return config;
	}
}
